package cn.itsource.org.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import cn.itsource.basic.service.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.RedisService;
import cn.itsource.config.exception.CustomException;
import cn.itsource.org.Dto.LoginDto;
import cn.itsource.org.Vo.LoginVo;
import cn.itsource.org.domain.Department;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.query.EmployeeQuery;
import cn.itsource.org.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service //spring管理
public class EmployeeImplServiceImpl extends BaseServiceImpl<Employee,EmployeeQuery> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private  RedisService redisService;

    //普通crud通过继承得到

    @Override
    public List<Employee> findManagers() {
        return employeeMapper.findManagers();
    }

    @Override
    public Employee accountingCheck(Employee admin) {
        return employeeMapper.accountingCheck(admin);
    }

    @Override
    public Employee emailCheck(Employee admin) {
        return employeeMapper.emailCheck(admin);
    }

    @Override
    public Employee phoneCheck(Employee admin) {
        return employeeMapper.phoneCheck(admin);
    }
//登录验证
    @Override
    public AjaxResult loginCheck(LoginDto dto) {

        //2.根据用户名进行匹配   //账户名与密码不匹配，请重新输入
        Employee employee = BeanUtil.copyProperties(dto, Employee.class);
        Employee dbEmployee = employeeMapper.loginCheck(employee);
        //3.如果匹配不到，返回错误
        if (dbEmployee == null){
            throw new CustomException("账户名与密码不匹配，请重新输入");
        }
        //4.如果匹配到了，对进来的密码串加盐加密和数据库的加密串进行比对，
        //4.1拿到数据库的盐值，再对页面传递过来的密码进行加密
        String pageMd5Pwd = MD5.create().setSalt(dbEmployee.getSalt().getBytes()).digestHex(dto.getPassword());
        System.out.println(pageMd5Pwd);

        //5.比对不过，返回错误  pageMd5Pwd:页面密码加密串   dbEmployee.getPassword():数据库的加密串
        if (!StrUtil.equals(pageMd5Pwd,dbEmployee.getPassword())){
            throw new CustomException("账户名与密码不匹配，请重新输入");
        }
        //6.成功，存放redis.setKv(token,用户信息，时间)
        String token = IdUtil.fastSimpleUUID();
        //6.1封装成一个通用对象，用户系统和后台系统都可以用这个
        LoginVo loginVo = BeanUtil.copyProperties(dbEmployee, LoginVo.class);
        loginVo.setToken(token);
        redisService.setStringKeyAndValue(token,loginVo,30);
        //7.返回vo对象，vo专门用来返回值给前端页面的
        return AjaxResult.createSuccess(loginVo);
    }


    @Override
    public PageList<Employee> findPage(EmployeeQuery query) {
        //1.分页插件的使用
        PageHelper.startPage(query.getPage(), query.getSize());
        //2.执行分页语句 1，2顺序不能反  ，中间不能写代码
        List<Employee> employees = employeeMapper.findPage(query);
        //3.封装到pageInfo里面去
        PageInfo<Employee> employeePageInfo = new PageInfo<>(employees);
        //4.封装自定义pageList对象
        PageList<Employee> employeePageList = new PageList<>(employeePageInfo.getTotal(), employeePageInfo.getList());
        return employeePageList;
    }

}
